<template>
	<view class="page">
		<view class="top">
			<u-tabs :list="list" :is-scroll="false" :current="current" @change="change" active-color="#6bc665"></u-tabs>
		</view>
		<!-- 侧边导航 -->
		<!-- <view class="slider">
			<view class="item" v-for="(item,index) in slider" :key="index" @click="selectSlider(item.id)"
				:class="{active:item.id==sliderId}">
				{{item.name}}
			</view>
			<view class="silderBlock" :style="style">

			</view>
		</view> -->
		<!-- 内容 -->
		<view class="goods" v-if="goods.length>0">
			<view class="item" v-for="(item,index) in goods" :key="index">
				<view class="top">
					<view class="left">
						<image :src="item.images_format[0]" mode="widthFix" style="width: 160rpx;height: 160rpx;">
						</image>
					</view>
					<view class="right">
						<view class="name">
							{{item.name}}
						</view>
						<view class="num">
							<span>库存 {{item.stock}}</span> <span>销量 {{item.sale_count}}</span>
						</view>
						<view class="price">
							<span style="font-size: 22rpx;">￥</span>{{item.price}}
						</view>
					</view>
				</view>
				<view class="bottom">
					<view class="selectCz" v-if="item.is_sale==0" @click="setGoods(item)">
						上架
					</view>
					<view class="selectCz" v-if="item.is_sale==1" @click="setGoods(item)">
						下架
					</view>
				</view>
			</view>
		</view>
		<view class="noDAta" style="width: 410rpx;height: 240rpx; margin: 60rpx auto;">
			<image src="../../static/empty.png" mode="widthFix" style="width: 410rpx;"></image>
		</view>
	</view>
</template>

<script>
	import {
		GetGoodsList
	} from '@/api/user.js'
	import {
		uDgoods
	} from '@/api/index.js'
	export default {
		data() {
			return {
				// 顶部导航
				list: [{
						name: '全部'
					}, {
						name: '销售中'
					},
					{
						name: '已下架'
					},
					{
						name: '已售罄'
					}
				],
				// 顶部
				current: 0,
				// 侧边
				slider: [{
					id: 0,
					name: "推荐分类"
				}, {
					id: 1,
					name: "栏目分类"
				}],
				sliderId: 0,
				style: "",
				goods: [],
				chainId: ""

			};
		},
		onLoad(res) {
			this.chainId = uni.getStorageSync("chainId")
			let id = Number(res.id) + 1
			this.change(id)
		},
		methods: {
			initData(obj) {
				console.log("数据", this.chainId);
				GetGoodsList(obj).then(res => {
					console.log(res);
					this.goods = res.data
				})
			},
			change(v) {
				// console.log(v);
				this.current = v
				let obj = {}
				if (v == 0) {
					obj = {}
					obj.id = this.chainId
				}
				if (v == 1) {
					obj = {}
					obj.id = this.chainId
					obj.is_sale = 1
				}
				if (v == 2) {
					obj = {}
					obj.id = this.chainId
					obj.is_sale = 2
				}
				if (v == 3) {
					obj = {}
					obj.id = this.chainId
					obj.stockout = 1
				}
				this.initData(obj)

			},
			selectSlider(id) {
				this.sliderId = id
				let jl = Number(id)
				this.style = {
					transform: `translateY(${jl*100}rpx)`
				}
			},
			setGoods(item) {
				if (item.is_sale == 0) {
					uDgoods({
						id: item.id,
						status: 1
					}).then(res => {
						if(res.code==200){
							uni.showToast({
								title: "上架成功",
								icon: "none"
							})
						}
						this.change(this.current)
						
					})
				} else {
					uDgoods({
						id: item.id,
						status: 0
					}).then(res => {
						if (res.code == 200) {
							uni.showToast({
								title: "下架成功",
								icon: "none"
							})
						}
						this.change(this.current)

					})
				}

			}
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		min-width: 100vw;
		background-color: #f5f5f5;
		min-height: 100vh;
		font-family: Source Han Sans CN;
		font-weight: 400;
	}

	.slider {
		position: fixed;
		left: 0;
		top: 120rpx;
		display: flex;
		flex-direction: column;
		text-align: center;
		background: #F5F5F5;
		font-size: 28rpx;
		width: 176rpx;
		height: calc(100vh - 120rpx);

		.item {
			width: 176rpx;
			height: 100rpx;
			font-size: 28rpx;
			font-family: Source Han Sans CN;
			font-weight: 400;
			line-height: 100rpx;
		}

		.item.active {
			background: #E7F5E6;
			color: #6BC665;
		}

		.silderBlock {
			position: absolute;
			top: 0;
			left: 2rpx;
			width: 4rpx;
			height: 100rpx;
			background-color: #6BC665FF;
			transition: all linear 0.1s;
		}
	}

	.goods {
		position: fixed;
		left: 0rpx;
		top: 100rpx;
		width: calc(100vw - 0rpx);
		height: calc(100vh - 100rpx);
		box-sizing: border-box;
		background-color: #fff;
		padding: 20rpx;
		overflow: scroll;

		.item {
			border-bottom: 1rpx solid #e6e6e6;
			padding: 10rpx 0 20rpx 0;
		}

		.item .top {
			display: flex;

			.left {
				width: 160rpx;
				height: 160rpx;

				border-radius: 10rpx;
				overflow: hidden;
			}

			.right {
				width: calc(100% - 160rpx);
				height: 160rpx;
				box-sizing: border-box;
				padding: 10rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
			}

			.right .name {
				font-size: 26rpx;
				color: #333333FF;
			}

			.right .num {
				display: flex;
				justify-content: space-between;
				color: #999999FF;
				font-size: 22rpx;
			}

			.right .price {
				color: #FA453CFF;
				font-size: 26rpx;
			}

		}

		.bottom {
			display: flex;
			flex-direction: row-reverse;

			.selectCz {
				width: 96rpx;
				height: 42rpx;
				border: 1px solid #DBDADC;
				border-radius: 5rpx;
				text-align: center;
				font-size: 24rpx;
				color: #333333FF;
				line-height: 42rpx;
			}
		}
	}
</style>